package com.xl98.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xl98.entity.*;
import com.xl98.service.PermissionService;
import com.xl98.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/api/perm")
@Api(value = "权限",tags = {"权限"})
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有权限类型")
    public Result selectAllPerm() {
        List<Permission> permissions = permissionService.selectAllPerm();
        if (StringUtils.checkValNotNull(permissions)) {
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,permissions);
        } else return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
    }


    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/addPerm", method = RequestMethod.POST)
    @ApiOperation(value = "增加一个权限类型")
    public Result addPerm(String name, String description) {
        Permission permission = new Permission();
        permission.setName(name);
        permission.setDescription(description);
        int rs = permissionService.addPerm(permission);
        if (rs>0) {
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
        } else return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/rolePerm", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色所有权限类型")
    public Result selectRolePermissionByRoleId(String roleId) {
        List<Permission> permissions = permissionService.selectPermByRoleId(roleId);
        if (StringUtils.checkValNotNull(permissions)) {
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,permissions);
        } else return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/addRolePerm", method = RequestMethod.POST)
    @ApiOperation(value = "为角色增加一个权限类型")
    public Result addRolePerm(String roleId, String permId) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permId);
        int rs = rolePermissionService.addRolePerm(rolePermission);
        if (rs>0) {
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
        } else return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
    }
}

